package com.jkl.service.home;

import com.jkl.dao.home.CapabilityAnalysisMapper;
import com.jkl.entity.dept.BSDept;
import com.jkl.entity.home.CapabilityResponseEntity;
import com.jkl.entity.home.HomeCapabilityMappingEntity;
import com.jkl.entity.home.HomeRequestEntity;
import com.jkl.service.dept.BSDeptService;
import com.jkl.service.home.capabilityAnalysisInter.CapabilityAnalysisServiceNew;
import com.jkl.untils.CommonUtil;
import com.jkl.untils.DateUtil;
import com.jkl.untils.GetIntUtil;
import com.jkl.untils.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author zjj
 * @date  2019/9/4 14:30
 * @Description  ${描述一个类的相关信息}
 */
@Service
public class CapabilityAnalysisServiceNewImpl implements CapabilityAnalysisServiceNew {
    @Autowired
    CapabilityAnalysisMapper capabilityAnalysisDao;

    @Autowired
    private BSDeptService bsDeptService;

    @Override
    public CapabilityResponseEntity getAbilityBalanceAnalysis(HomeRequestEntity homeRequestEntity) {
        HomeCapabilityMappingEntity abilityBalanceAnalysis = capabilityAnalysisDao.getAbilityBalanceAnalysis(homeRequestEntity);
        if (abilityBalanceAnalysis!=null) {
            CapabilityResponseEntity capabilityResponseEntity = new CapabilityResponseEntity();
            capabilityResponseEntity.setKitchenDesigne(GetIntUtil.getInt(abilityBalanceAnalysis.getKitchenDesigne()));
            capabilityResponseEntity.setIncinerationDesigne(GetIntUtil.getInt(abilityBalanceAnalysis.getIncinerationDesigne()));
            capabilityResponseEntity.setLandfillDesigne(GetIntUtil.getInt(abilityBalanceAnalysis.getLandfillDesigne()));
            capabilityResponseEntity.setKitchenDisposedTotal(GetIntUtil.getInt(abilityBalanceAnalysis.getKitchenDisposedTotal()));
            capabilityResponseEntity.setAreaName(abilityBalanceAnalysis.getAreaName());
            capabilityResponseEntity.setIncinerationDisposedTotal(GetIntUtil.getInt(abilityBalanceAnalysis.getIncinerationDisposedTotal()));
            capabilityResponseEntity.setLandfillDisposedTotal(GetIntUtil.getInt(abilityBalanceAnalysis.getLandfillDisposedTotal()));
            return capabilityResponseEntity;
        }else {
            CapabilityResponseEntity capabilityResponseEntity = new CapabilityResponseEntity();
            capabilityResponseEntity.setKitchenDesigne(0);
            capabilityResponseEntity.setIncinerationDesigne(0);
            capabilityResponseEntity.setLandfillDesigne(0);
            capabilityResponseEntity.setKitchenDisposedTotal(0);
            capabilityResponseEntity.setAreaName("");
            capabilityResponseEntity.setIncinerationDisposedTotal(0);
            capabilityResponseEntity.setLandfillDisposedTotal(0);
            return capabilityResponseEntity;
        }
    }

    @Override
    public Map<String, Object> getDisposalStructureAnalysis(HomeRequestEntity homeRequestEntity) {
        String startTime = LocalDate.now().minusMonths(1).minusYears(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        String endTime = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        homeRequestEntity.setStartTime(startTime);
        homeRequestEntity.setEndTime(endTime);
        HomeCapabilityMappingEntity disposalStructureAnalysisCircle = capabilityAnalysisDao.getDisposalStructureAnalysisCircle(homeRequestEntity);
        List<HomeCapabilityMappingEntity> disposalStructureAnalysisList = capabilityAnalysisDao.getDisposalStructureAnalysisList(homeRequestEntity);;
        Map<String, Object> map = new HashMap<>();
        List<CapabilityResponseEntity> list = new ArrayList<>();
        //单位转换
        int disposed = new BigDecimal(disposalStructureAnalysisCircle.getDisposed()!=null?disposalStructureAnalysisCircle.getDisposed():0).divide(new BigDecimal(10000), 0).intValue();
        int locationDisposed = new BigDecimal(disposalStructureAnalysisCircle.getLocationDisposed()!=null?disposalStructureAnalysisCircle.getLocationDisposed():0).divide(new BigDecimal(10000), 0).intValue();
        int kitchen = new BigDecimal(disposalStructureAnalysisCircle.getKitchen()!=null?disposalStructureAnalysisCircle.getKitchen():0).divide(new BigDecimal(10000), 0).intValue();
        int landfill = new BigDecimal(disposalStructureAnalysisCircle.getLandfill()!=null?disposalStructureAnalysisCircle.getLandfill():0).divide(new BigDecimal(10000), 0).intValue();
        int incineration = new BigDecimal(disposalStructureAnalysisCircle.getIncineration()!=null?disposalStructureAnalysisCircle.getIncineration():0).divide(new BigDecimal(10000), 0).intValue();
        CapabilityResponseEntity capabilityResponseEntity = new CapabilityResponseEntity();
        capabilityResponseEntity.setIncinerationRadio(incineration);
        capabilityResponseEntity.setLocationDisposedRadio(locationDisposed);
        capabilityResponseEntity.setKitchenRadio(kitchen);
        capabilityResponseEntity.setLandfillRadio(landfill);
        capabilityResponseEntity.setDisposed(disposed);
        capabilityResponseEntity.setStartTime(startTime);
        capabilityResponseEntity.setEndTime(endTime);
        capabilityResponseEntity.setAreaName(disposalStructureAnalysisCircle.getAreaName());

        for (HomeCapabilityMappingEntity homeCapabilityMappingEntity : disposalStructureAnalysisList) {
            int kitchenRadio=0;
            int incinerationRadio=0;
            int landfillRadio=0;
            int localDisposedRadio=0;
            //餐厨量
            int kitchenTotal = new BigDecimal(homeCapabilityMappingEntity.getKitchen()==null?0:homeCapabilityMappingEntity.getKitchen()).divide(new BigDecimal(10000), 0).intValue();
            //焚烧量
            int incinerationTotal = new BigDecimal(homeCapabilityMappingEntity.getIncineration()==null?0:homeCapabilityMappingEntity.getIncineration()).divide(new BigDecimal(10000), 0).intValue();
            //填埋量
            int landfillTotal = new BigDecimal(homeCapabilityMappingEntity.getLandfill()==null?0:homeCapabilityMappingEntity.getLandfill()).divide(new BigDecimal(10000), 0).intValue();
            //就地处置量
            int localDisposedTotal = new BigDecimal(homeCapabilityMappingEntity.getLocationDisposed()==null?0:homeCapabilityMappingEntity.getLocationDisposed()).divide(new BigDecimal(10000), 0).intValue();

            if (homeCapabilityMappingEntity.getDisposed()!=null && homeCapabilityMappingEntity.getDisposed().intValue()!=0) {
                //餐厨占比
                kitchenRadio = new BigDecimal(homeCapabilityMappingEntity.getKitchen()==null?0:homeCapabilityMappingEntity.getKitchen()).divide(new BigDecimal(homeCapabilityMappingEntity.getDisposed()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
                //焚烧厂占比
               incinerationRadio = new BigDecimal(homeCapabilityMappingEntity.getIncineration()==null?0:homeCapabilityMappingEntity.getIncineration()).divide(new BigDecimal(homeCapabilityMappingEntity.getDisposed()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
               //填埋厂占比
                landfillRadio = new BigDecimal(homeCapabilityMappingEntity.getLandfill()==null?0:homeCapabilityMappingEntity.getLandfill()).divide(new BigDecimal(homeCapabilityMappingEntity.getDisposed()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
                //就地处置占比
                localDisposedRadio = new BigDecimal(homeCapabilityMappingEntity.getLocationDisposed()==null?0:homeCapabilityMappingEntity.getLocationDisposed()).divide(new BigDecimal(homeCapabilityMappingEntity.getDisposed()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();

            }
            CapabilityResponseEntity capabilityResponseEntity1 = new CapabilityResponseEntity();
            capabilityResponseEntity1.setAreaName(homeCapabilityMappingEntity.getAreaName());
            capabilityResponseEntity1.setAreaCode(homeCapabilityMappingEntity.getAreaCode());
            capabilityResponseEntity1.setKitchen(kitchenTotal);
            capabilityResponseEntity1.setKitchenRadio(kitchenRadio);
            capabilityResponseEntity1.setIncinerationRadio(incinerationRadio);
            capabilityResponseEntity1.setIncineration(incinerationTotal);
            capabilityResponseEntity1.setLandfillRadio(landfillRadio);
            capabilityResponseEntity1.setLocationDisposedRadio(localDisposedRadio);
            capabilityResponseEntity1.setLandfill(landfillTotal);
            capabilityResponseEntity1.setLocationDisposed(localDisposedTotal);
            list.add(capabilityResponseEntity1);
        }
        map.put("circle", capabilityResponseEntity);
        map.put("list", list);

        return map;
    }

    @Override
    public CapabilityResponseEntity getConstructionProgressCircle(HomeRequestEntity homeRequestEntity) {
        HomeCapabilityMappingEntity constructionProgressCircle = capabilityAnalysisDao.getConstructionProgressCircle(homeRequestEntity);
        //焚烧厂完成占比
        int incinerationRadio=0;
        if (constructionProgressCircle.getIncinerationAimCount()!=null &&  constructionProgressCircle.getIncinerationAimCount().intValue()!=0) {
             incinerationRadio = new BigDecimal(constructionProgressCircle.getIncinerationFinishCount() != null ? constructionProgressCircle.getIncinerationFinishCount() : 0).divide(new BigDecimal(constructionProgressCircle.getIncinerationAimCount()),2,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
        }
        //餐厨厂占比
        int kitchenRadio=0;
        if (constructionProgressCircle.getKitchenAimCount() != null && constructionProgressCircle.getKitchenAimCount().intValue()!= 0) {
            kitchenRadio = new BigDecimal(constructionProgressCircle.getKitchenFinishCount() != null ? constructionProgressCircle.getKitchenFinishCount() : 0).divide(new BigDecimal(constructionProgressCircle.getKitchenAimCount()),2,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
        }
        CapabilityResponseEntity capabilityResponseEntity = new CapabilityResponseEntity();
        capabilityResponseEntity.setKitchenAimCount(GetIntUtil.getInt(constructionProgressCircle.getKitchenAimCount()!=null?constructionProgressCircle.getKitchenAimCount():0));
        capabilityResponseEntity.setKitchenCompleteRadio(kitchenRadio);
        capabilityResponseEntity.setIncinerationAimCount(GetIntUtil.getInt(constructionProgressCircle.getIncinerationAimCount()!=null?constructionProgressCircle.getIncinerationAimCount():0));
        capabilityResponseEntity.setIncinerationCompleteRadio(incinerationRadio);
        return capabilityResponseEntity;
    }

    @Override
    public List<CapabilityResponseEntity> getConstructionProgressHistogram(HomeRequestEntity homeRequestEntity) {
        String  endTime= LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        String startTime = LocalDate.now().minusYears(3).getYear() + "-01";
        homeRequestEntity.setEndTime(endTime);
        homeRequestEntity.setStartTime(startTime);
        List<HomeCapabilityMappingEntity> constructionProgressHistogram = capabilityAnalysisDao.getConstructionProgressHistogram(homeRequestEntity);
        List<CapabilityResponseEntity> list = new ArrayList<>();
        if (constructionProgressHistogram!=null) {
            for (HomeCapabilityMappingEntity homeCapabilityMappingEntity : constructionProgressHistogram) {
                CapabilityResponseEntity capabilityResponseEntity = new CapabilityResponseEntity();
                capabilityResponseEntity.setYear(homeCapabilityMappingEntity.getYear());
                capabilityResponseEntity.setKitchenRealAmount(GetIntUtil.getInt(homeCapabilityMappingEntity.getKitchenRealAmount()));
                capabilityResponseEntity.setKitchenAimHandle(GetIntUtil.getInt(homeCapabilityMappingEntity.getKitchenAimHandle()));
                capabilityResponseEntity.setIncinerationRealAmount(GetIntUtil.getInt(homeCapabilityMappingEntity.getIncinerationRealAmount()));
                capabilityResponseEntity.setIncinerationAimAmount(GetIntUtil.getInt(homeCapabilityMappingEntity.getIncinerationAimAmount()));
                list.add(capabilityResponseEntity);
            }
        }
        return list;
    }

    @Override
    public List<CapabilityResponseEntity> getProcessingForecast(HomeRequestEntity homeRequestEntity) {
        String endTime = LocalDate.now().plusYears(1).getYear() + "";
        String startTime = LocalDate.now().minusYears(1).getYear() + "-01";
        homeRequestEntity.setStartTime(startTime);
        homeRequestEntity.setEndTime(endTime);
        List<HomeCapabilityMappingEntity> processingForecast = capabilityAnalysisDao.getProcessingForecast(homeRequestEntity);
        List<CapabilityResponseEntity> list = new ArrayList<>();
        if (processingForecast!=null) {
            for (HomeCapabilityMappingEntity homeCapabilityMappingEntity : processingForecast) {
                CapabilityResponseEntity capabilityResponseEntity = new CapabilityResponseEntity();
                capabilityResponseEntity.setYear(homeCapabilityMappingEntity.getYear()+"年");
                capabilityResponseEntity.setKitchenAmount(GetIntUtil.getInt(homeCapabilityMappingEntity.getKitchenAmount()));
                capabilityResponseEntity.setIncinerationAmount(GetIntUtil.getInt(homeCapabilityMappingEntity.getIncinerationAmount()));
                capabilityResponseEntity.setLandFillAmount(GetIntUtil.getInt(homeCapabilityMappingEntity.getLandFillAmount()));
                capabilityResponseEntity.setKitchenDesigne(GetIntUtil.getInt(homeCapabilityMappingEntity.getKitchenDesigne()));
                capabilityResponseEntity.setIncinerationDesigne(GetIntUtil.getInt(homeCapabilityMappingEntity.getIncinerationDesigne()));
                capabilityResponseEntity.setLandfillDesigne(GetIntUtil.getInt(homeCapabilityMappingEntity.getLandfillDesigne()));
                list.add(capabilityResponseEntity);
            }
        }
        return list;

    }

    @Override
    public CapabilityResponseEntity getGis(HomeRequestEntity homeRequestEntity) {
        HomeCapabilityMappingEntity gis = capabilityAnalysisDao.getGis(homeRequestEntity);
        int incineration = 0;
        int landfill = 0;
        int kitchen = 0;
        //焚烧厂负荷率
        if (gis.getIncinerationDesigne()!=null&& gis.getIncinerationDesigne().intValue()!=0) {
            incineration= new BigDecimal(gis.getIncinerationAmount() != null ? gis.getIncinerationAmount() : 0).divide(new BigDecimal(gis.getIncinerationDesigne()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
        }
        //餐厨厂负荷率
        if (gis.getKitchenDesigne()!=null&& gis.getKitchenDesigne().intValue()!=0) {
            kitchen= new BigDecimal(gis.getKitchenAmount() != null ? gis.getKitchenAmount() : 0).divide(new BigDecimal(gis.getKitchenDesigne()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
        }
        //填埋负荷率
        if (gis.getLandfillDesigne()!=null&& gis.getLandfillDesigne().intValue()!=0) {
            landfill= new BigDecimal(gis.getLandFillAmount() != null ? gis.getLandFillAmount() : 0).divide(new BigDecimal(gis.getLandfillDesigne()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
        }
        CapabilityResponseEntity capabilityResponseEntity = new CapabilityResponseEntity();
        capabilityResponseEntity.setIncinerationLoadFactor(incineration);
        capabilityResponseEntity.setKitchenLoadFactor(kitchen);
        capabilityResponseEntity.setLandFillLoadFactor(landfill);
        return capabilityResponseEntity;

    }

    @Override
    public CapabilityResponseEntity getGisNew(HomeRequestEntity homeRequestEntity) {
        if (StringUtils.isEmpty(homeRequestEntity.getYear())) {
            String newYear = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().plusYears(0).format(DateTimeFormatter.ofPattern("yyyy"));
            String lastYear = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().plusYears(-1).format(DateTimeFormatter.ofPattern("yyyy"));
            homeRequestEntity.setYear(lastYear);
            String mon=DateUtil.getOldMon();
            if(mon.equals(lastYear+"-12")){
                homeRequestEntity.setStartTime(homeRequestEntity.getYear() + "-01");
                homeRequestEntity.setEndTime(homeRequestEntity.getYear()+"-12");
            }else{
                homeRequestEntity.setStartTime(newYear + "-01");
                homeRequestEntity.setEndTime(mon);
            }
        }
        HomeCapabilityMappingEntity gis = capabilityAnalysisDao.getGisNewOne(homeRequestEntity);
        List<HomeCapabilityMappingEntity> resultDesigne = capabilityAnalysisDao.allProvinceAnalysisTow(homeRequestEntity);
        List<HomeCapabilityMappingEntity> resultList = capabilityAnalysisDao.allProvinceAnalysisOne(homeRequestEntity);
        double incinerationDesigne=0;
        double kitchenDesigne=0;
        double landfillDesigne=0;
        for (HomeCapabilityMappingEntity homeCapabilityMappingEntity : resultDesigne) {
            if("103".equals(homeCapabilityMappingEntity.getProjectType())&&!StringUtils.isEmpty(homeCapabilityMappingEntity.getDesignHandle())){
                incinerationDesigne=homeCapabilityMappingEntity.getDesignHandle();
            }else if("104".equals(homeCapabilityMappingEntity.getProjectType())&&!StringUtils.isEmpty(homeCapabilityMappingEntity.getDesignHandle())){
                kitchenDesigne=homeCapabilityMappingEntity.getDesignHandle();
            }else if("105".equals(homeCapabilityMappingEntity.getProjectType())&&!StringUtils.isEmpty(homeCapabilityMappingEntity.getDesignHandle())){
                landfillDesigne=homeCapabilityMappingEntity.getDesignHandle();
            }
        }
        int incineration = 0;
        int landfill = 0;
        int kitchen = 0;
        //焚烧厂负荷率
        if (null!=gis&&incinerationDesigne!=0) {
            incinerationDesigne=incinerationDesigne*resultList.size();
            incineration= new BigDecimal(gis.getIncinerationAmount() != null ? gis.getIncinerationAmount() : 0).divide(new BigDecimal(incinerationDesigne), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
        }
        //餐厨厂负荷率
        if (null!=gis&&kitchenDesigne!=0) {
            kitchenDesigne=kitchenDesigne*resultList.size();
            kitchen= new BigDecimal(gis.getKitchenAmount() != null ? gis.getKitchenAmount() : 0).divide(new BigDecimal(kitchenDesigne), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
        }
        //填埋负荷率
        if (null!=gis&&landfillDesigne!=0) {
            landfillDesigne=landfillDesigne*resultList.size();
            landfill= new BigDecimal(gis.getLandFillAmount() != null ? gis.getLandFillAmount() : 0).divide(new BigDecimal(landfillDesigne), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
        }
        CapabilityResponseEntity capabilityResponseEntity = new CapabilityResponseEntity();
        capabilityResponseEntity.setIncinerationLoadFactor(incineration);
        capabilityResponseEntity.setKitchenLoadFactor(kitchen);
        capabilityResponseEntity.setLandFillLoadFactor(landfill);
        return capabilityResponseEntity;
    }

    @Override
    public List<HomeCapabilityMappingEntity> allProvinceAnalysis(HomeRequestEntity homeRequestEntity) throws ParseException {
        SimpleDateFormat formatMonth = new SimpleDateFormat(DateUtil.DATE_FORMAT_MONTH);
        SimpleDateFormat formatYear = new SimpleDateFormat(DateUtil.DATE_FORMAT_YEAR);
        DateTimeFormatter formatterMonth = DateTimeFormatter.ofPattern(DateUtil.DATE_FORMAT_MONTH);
        DateTimeFormatter formatterYear = DateTimeFormatter.ofPattern(DateUtil.DATE_FORMAT_YEAR);
        String startTime = LocalDate.now().minusYears(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        String endTime = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        if (StringUtils.isEmpty(homeRequestEntity.getEndTime()) && StringUtils.isEmpty(homeRequestEntity.getStartTime())) {
            if (startTime.compareTo("2019-09") < 0) {
                homeRequestEntity.setEndTime("2020-08");
                homeRequestEntity.setStartTime("2019-09");
            } else {
                homeRequestEntity.setEndTime(endTime);
                homeRequestEntity.setStartTime(startTime);
            }
        }
        List<HomeCapabilityMappingEntity> homeCapabilityMappingEntityList = new ArrayList<>();
//        homeRequestEntity.setStartYear(LocalDateTime.ofInstant(formatMonth.parse(homeRequestEntity.getStartTime()).toInstant(),ZoneId.systemDefault()).plusYears(-1).format(formatterYear));
//        homeRequestEntity.setEndYear(LocalDateTime.ofInstant(formatYear.parse(homeRequestEntity.getEndTime()).toInstant(),ZoneId.systemDefault()).plusYears(-1).format(formatterYear));
        String tempTime = homeRequestEntity.getStartTime();
        while (true) {
            HomeCapabilityMappingEntity entity = new HomeCapabilityMappingEntity();
            entity.setMonth(tempTime);
            entity.setIncinerationLoadFactor(0D);
            entity.setKitchenLoadFactor(0D);
            entity.setIncinerationAmount(0D);
            entity.setKitchenAmount(0D);
            entity.setKitchenDesigne(0D);
            entity.setIncinerationDesigne(0D);
            homeCapabilityMappingEntityList.add(entity);
            if (tempTime.equals(homeRequestEntity.getEndTime())) {
                break;
            }
            tempTime = LocalDateTime.ofInstant(formatMonth.parse(tempTime).toInstant(), ZoneId.systemDefault()).plusMonths(1).format(formatterMonth);
        }
        List<HomeCapabilityMappingEntity> resultList = capabilityAnalysisDao.allProvinceAnalysisOne(homeRequestEntity);
        List<HomeCapabilityMappingEntity> resultDesigne = capabilityAnalysisDao.allProvinceAnalysisTow(homeRequestEntity);
        double incinerationDesigne=0;
        double kitchenDesigne=0;
        for (HomeCapabilityMappingEntity homeCapabilityMappingEntity : resultDesigne) {
            if("103".equals(homeCapabilityMappingEntity.getProjectType())&&!StringUtils.isEmpty(homeCapabilityMappingEntity.getDesignHandle())){
                incinerationDesigne=homeCapabilityMappingEntity.getDesignHandle();
            }else if("104".equals(homeCapabilityMappingEntity.getProjectType())&&!StringUtils.isEmpty(homeCapabilityMappingEntity.getDesignHandle())){
                kitchenDesigne=homeCapabilityMappingEntity.getDesignHandle();
            }
        }
        for (HomeCapabilityMappingEntity homeCapabilityMappingEntity : resultList) {
            for (HomeCapabilityMappingEntity entity : homeCapabilityMappingEntityList) {
                    entity.setKitchenDesigne(CommonUtil.doublePoint(kitchenDesigne,2));
                    entity.setIncinerationDesigne(CommonUtil.doublePoint(incinerationDesigne,2));
                if (entity.getMonth().equals(homeCapabilityMappingEntity.getMonth())) {
                    double incineration = 0;
                    double kitchen = 0;
                    //焚烧厂负荷率
                    if (incinerationDesigne!=0) {
                        incineration= new BigDecimal(homeCapabilityMappingEntity.getIncinerationAmount() != null ?
                                homeCapabilityMappingEntity.getIncinerationAmount() : 0).divide(new BigDecimal(incinerationDesigne), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
                    }
                    //餐厨厂负荷率
                    if (kitchenDesigne!=0) {
                        kitchen= new BigDecimal(homeCapabilityMappingEntity.getKitchenAmount() != null ?
                                homeCapabilityMappingEntity.getKitchenAmount() : 0).divide(new BigDecimal(kitchenDesigne), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
                    }
                    if (!ObjectUtils.isEmpty(incineration)) {
                        entity.setIncinerationLoadFactor(incineration);
                    }
                    if (!ObjectUtils.isEmpty(kitchen)) {
                        entity.setKitchenLoadFactor(kitchen);
                    }

                    if (!ObjectUtils.isEmpty(homeCapabilityMappingEntity.getIncinerationAmount())) {
                        entity.setIncinerationAmount(CommonUtil.doublePoint(homeCapabilityMappingEntity.getIncinerationAmount(),2));
                    }
                    if (!ObjectUtils.isEmpty(homeCapabilityMappingEntity.getKitchenAmount())) {
                        entity.setKitchenAmount(CommonUtil.doublePoint(homeCapabilityMappingEntity.getKitchenAmount(),2));
                    }
                }
            }
        }
        return homeCapabilityMappingEntityList;
    }

    @Override
    public List<HomeCapabilityMappingEntity> cityAnalysis(HomeRequestEntity homeRequestEntity) throws ParseException {
        String code=homeRequestEntity.getAreaCode();
        SimpleDateFormat formatMonth = new SimpleDateFormat(DateUtil.DATE_FORMAT_MONTH);
        DateTimeFormatter formatterMonth = DateTimeFormatter.ofPattern(DateUtil.DATE_FORMAT_MONTH);
        DateTimeFormatter formatterYear = DateTimeFormatter.ofPattern(DateUtil.DATE_FORMAT_YEAR);
        if (StringUtils.isEmpty(homeRequestEntity.getStartTime())) {
            homeRequestEntity.setStartTime(LocalDateTime.now().plusMonths(-1).format(formatterMonth));
        }
        homeRequestEntity.setStartYear(LocalDateTime.ofInstant(formatMonth.parse(homeRequestEntity.getStartTime()).toInstant(),ZoneId.systemDefault()).plusYears(-1).format(formatterYear));
        homeRequestEntity.setAreaCode(StrUtil.addZeroRight(homeRequestEntity.getAreaCode(),6));
        List<BSDept> bsDepts = bsDeptService.queryByParentId(homeRequestEntity.getAreaCode());
        if (homeRequestEntity.getAreaCode().length() == 6 &&code.length()==6) {
            BSDept areaByDeptId = bsDeptService.getAreaByAreaId(homeRequestEntity.getAreaCode());
            bsDepts.add(areaByDeptId);
        }
        List<HomeCapabilityMappingEntity> resultList = new ArrayList<>();
        for (BSDept bsDept : bsDepts) {
            String deptCode = bsDept.getDeptCode();
            homeRequestEntity.setAreaCode(removeLast00(deptCode));
            HomeCapabilityMappingEntity entity = new HomeCapabilityMappingEntity();
            entity.setIncinerationLoadFactor(0D);
            entity.setKitchenLoadFactor(0D);
            entity.setKitchenAmount(0D);
            entity.setKitchenDesigne(0D);
            entity.setIncinerationDesigne(0D);
            entity.setIncinerationAmount(0D);
            entity.setAreaName(bsDept.getDeptName());
            entity.setAreaCode(deptCode);
            resultList.add(entity);
            HomeCapabilityMappingEntity homeCapabilityMappingEntity = capabilityAnalysisDao.cityAnalysis(homeRequestEntity);
            List<HomeCapabilityMappingEntity> resultDesigne = capabilityAnalysisDao.allProvinceAnalysisTow(homeRequestEntity);
            double incinerationDesigne=0;
            double kitchenDesigne=0;
            for (HomeCapabilityMappingEntity homeCapabilityMappingEntitys : resultDesigne) {
                if("103".equals(homeCapabilityMappingEntitys.getProjectType())&&!StringUtils.isEmpty(homeCapabilityMappingEntitys.getDesignHandle())){
                    incinerationDesigne=homeCapabilityMappingEntitys.getDesignHandle();
                }else if("104".equals(homeCapabilityMappingEntitys.getProjectType())&&!StringUtils.isEmpty(homeCapabilityMappingEntitys.getDesignHandle())){
                    kitchenDesigne=homeCapabilityMappingEntitys.getDesignHandle();
                }
            }
            entity.setKitchenAmount(CommonUtil.doublePoint(homeCapabilityMappingEntity.getKitchenAmount(),2));
            entity.setIncinerationAmount(CommonUtil.doublePoint(homeCapabilityMappingEntity.getIncinerationAmount(),2));
            entity.setKitchenDesigne(CommonUtil.doublePoint(kitchenDesigne,2));
            entity.setIncinerationDesigne(CommonUtil.doublePoint(incinerationDesigne,2));
            double incineration = 0;
            double kitchen = 0;
            //焚烧厂负荷率
            if (incinerationDesigne!=0) {
                incineration= new BigDecimal(homeCapabilityMappingEntity.getIncinerationAmount() != null ?
                        homeCapabilityMappingEntity.getIncinerationAmount() : 0).divide(new BigDecimal(incinerationDesigne), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
            }
            //餐厨厂负荷率
            if (kitchenDesigne!=0) {
                kitchen= new BigDecimal(homeCapabilityMappingEntity.getKitchenAmount() != null ?
                        homeCapabilityMappingEntity.getKitchenAmount() : 0).divide(new BigDecimal(kitchenDesigne), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
            }
            entity.setIncinerationLoadFactor(incineration);
            entity.setKitchenLoadFactor(kitchen);
        }
        return resultList;
    }

    @Override
    public List<HomeCapabilityMappingEntity> shipEquipmentAnalysis(HomeRequestEntity homeRequestEntity) {
        List<HomeCapabilityMappingEntity> resultList = new ArrayList<>();
        String areaCode = StrUtil.addZeroRight(homeRequestEntity.getAreaCode(), 6);
        List<BSDept> bsDepts = bsDeptService.queryByParentId(areaCode);

        HomeCapabilityMappingEntity sumResult = null;
        BSDept dept = bsDeptService.getAreaByDeptCode(areaCode);
        String parentCode = String.valueOf(dept.getParentId());

        String areaCode2 = removeLast00(parentCode);
        homeRequestEntity.setAreaCode(removeLast00(homeRequestEntity.getAreaCode()));
        if (homeRequestEntity.getAreaCode().length() == 6) {
            bsDepts.add(dept);
        }else {
            sumResult = new HomeCapabilityMappingEntity();
            sumResult.setParentAreaCode(areaCode2);
            sumResult.setAreaCode(homeRequestEntity.getAreaCode());
            sumResult.setAreaName(dept.getDeptName());
            sumResult.setDecayVehicleSumThree(0);
            sumResult.setDecayVehicleSumEight(0);
            sumResult.setDecayVehicleSumOther(0);
            sumResult.setOtherVehicleSumThree(0);
            sumResult.setOtherVehicleSumEight(0);
            sumResult.setOtherVehicleSumOther(0);
            sumResult.setTransitSum(0);
            sumResult.setGatherSum(0);
            resultList.add(sumResult);
        }


        for (BSDept bsDept : bsDepts) {
            HomeCapabilityMappingEntity result = new HomeCapabilityMappingEntity();
            result.setAreaCode(bsDept.getDeptCode());
            result.setAreaName(bsDept.getDeptName());
            result.setDecayVehicleSumThree(0);
            result.setDecayVehicleSumEight(0);
            result.setDecayVehicleSumOther(0);
            result.setOtherVehicleSumThree(0);
            result.setOtherVehicleSumEight(0);
            result.setOtherVehicleSumOther(0);
            result.setTransitSum(0);
            result.setGatherSum(0);
            resultList.add(result);
            String areaCode1 = String.valueOf(bsDept.getDeptCode());

            homeRequestEntity.setAreaCode(removeLast00(areaCode1));
            List<HomeCapabilityMappingEntity> homeCapabilityMappingEntities = capabilityAnalysisDao.shipEquipmentCarAnalysis(homeRequestEntity);
            for (HomeCapabilityMappingEntity entity : homeCapabilityMappingEntities) {
                if ("303".equals(entity.getVehicleType())) {
                    //易腐
                    if ("401".equals(entity.getJobTonnage()) ||
                            "402".equals(entity.getJobTonnage())) {
                        //3-5吨
                        result.setDecayVehicleSumThree(entity.getCount() + result.getDecayVehicleSumThree());
                        if (!ObjectUtils.isEmpty(sumResult)) {

                            sumResult.setDecayVehicleSumThree(entity.getCount() + sumResult.getDecayVehicleSumThree());
                        }
                    }
                    if ("403".equals(entity.getJobTonnage()) ||
                            "404".equals(entity.getJobTonnage()) ||
                            "405".equals(entity.getJobTonnage())) {
                        //8-12吨
                        result.setDecayVehicleSumEight(entity.getCount() + result.getDecayVehicleSumEight());
                        if (!ObjectUtils.isEmpty(sumResult)) {

                            sumResult.setDecayVehicleSumEight(entity.getCount() + sumResult.getDecayVehicleSumEight());
                        }
                    }
                    if ("406".equals(entity.getJobTonnage())) {
                        //其他吨位
                        result.setDecayVehicleSumOther(entity.getCount() + result.getDecayVehicleSumOther());
                        if (!ObjectUtils.isEmpty(sumResult)) {

                            sumResult.setDecayVehicleSumOther(entity.getCount() + sumResult.getDecayVehicleSumOther());
                        }
                    }
                } else {
                    //其他
                    if ("401".equals(entity.getJobTonnage()) ||
                            "402".equals(entity.getJobTonnage())) {
                        //3-5吨
                        result.setOtherVehicleSumThree(entity.getCount() + result.getOtherVehicleSumThree());
                        if (!ObjectUtils.isEmpty(sumResult)) {

                            sumResult.setOtherVehicleSumThree(entity.getCount() + sumResult.getOtherVehicleSumThree());
                        }
                    }
                    if ("403".equals(entity.getJobTonnage()) ||
                            "404".equals(entity.getJobTonnage()) ||
                            "405".equals(entity.getJobTonnage())) {
                        //8-12吨
                        result.setOtherVehicleSumEight(entity.getCount() + result.getOtherVehicleSumEight());
                        if (!ObjectUtils.isEmpty(sumResult)) {

                            sumResult.setOtherVehicleSumEight(entity.getCount() + sumResult.getOtherVehicleSumEight());
                        }
                    }
                    if ("406".equals(entity.getJobTonnage())) {
                        //其他吨位
                        result.setOtherVehicleSumOther(entity.getCount() + result.getOtherVehicleSumOther());
                        if (!ObjectUtils.isEmpty(sumResult)) {

                            sumResult.setOtherVehicleSumOther(entity.getCount() + sumResult.getOtherVehicleSumOther());
                        }
                    }
                }

            }
            List<HomeCapabilityMappingEntity> homeCapabilityMappingEntities1 = capabilityAnalysisDao.shipEquipmentProjectAnalysis(homeRequestEntity);
            for (HomeCapabilityMappingEntity entity : homeCapabilityMappingEntities1) {
                if ("101".equals(entity.getProjectType())) {
                    //收集点
                    result.setGatherSum(entity.getCount() + result.getGatherSum());
                    if (!ObjectUtils.isEmpty(sumResult)) {

                        sumResult.setGatherSum(entity.getCount() + sumResult.getGatherSum());
                    }
                }
                //中转站
                if ("102".equals(entity.getProjectType())) {
                    //3-5吨
                    result.setTransitSum(entity.getCount() + result.getTransitSum());
                    if (!ObjectUtils.isEmpty(sumResult)) {

                        sumResult.setTransitSum(entity.getCount() + sumResult.getTransitSum());
                    }
                }

            }
        }
        return resultList;
    }

    @Override
    public List<HomeCapabilityMappingEntity> endDeal(HomeRequestEntity homeRequestEntity) {
        List<HomeCapabilityMappingEntity> resultList = new ArrayList<>();
        String areaCode = StrUtil.addZeroRight(homeRequestEntity.getAreaCode(), 6);
        List<BSDept> bsDepts = bsDeptService.queryByParentId(areaCode);

        String lastMonth = LocalDateTime.now().plusMonths(-1).format(DateTimeFormatter.ofPattern(DateUtil.DATE_FORMAT_MONTH));

        BSDept dept = bsDeptService.getAreaByDeptCode(areaCode);
        HomeCapabilityMappingEntity sumResult = null;
        String parentCode = String.valueOf(dept.getParentId());

        String areaCode3 = removeLast00(parentCode);
        homeRequestEntity.setAreaCode(removeLast00(homeRequestEntity.getAreaCode()));
        if (homeRequestEntity.getAreaCode().length() == 6) {
            bsDepts.add(dept);
        }else {
            sumResult = new HomeCapabilityMappingEntity();
            sumResult.setParentAreaCode(areaCode3);
            sumResult.setAreaCode(dept.getDeptCode());
            sumResult.setAreaName(dept.getDeptName());
            sumResult.setKitchenFinishCount(0D);
            sumResult.setKitchenAimHandle(0D);
            sumResult.setIncinerationAimAmount(0D);
            sumResult.setIncinerationFinishCount(0D);
            sumResult.setLocationDisposed(0D);
            resultList.add(sumResult);
        }

        if (homeRequestEntity.getAreaCode().length() == 2) {
            homeRequestEntity.setLevel("2");
        }
        if (homeRequestEntity.getAreaCode().length() == 4) {
            homeRequestEntity.setLevel("3");
        }
        if (homeRequestEntity.getAreaCode().length() == 6) {
            homeRequestEntity.setLevel("4");
        }
        for (BSDept bsDept : bsDepts) {
            HomeCapabilityMappingEntity result = new HomeCapabilityMappingEntity();
            result.setAreaCode(bsDept.getDeptCode());
            result.setAreaName(bsDept.getDeptName());
            result.setKitchenFinishCount(0D);
            result.setKitchenAimHandle(0D);
            result.setIncinerationAimAmount(0D);
            result.setIncinerationFinishCount(0D);
            result.setLocationDisposed(0D);
            resultList.add(result);

            String code = String.valueOf(bsDept.getDeptCode());

            homeRequestEntity.setAreaCode(removeLast00(code));
            List<HomeCapabilityMappingEntity> homeCapabilityMappingEntities = capabilityAnalysisDao.endDeal(homeRequestEntity);
            for (HomeCapabilityMappingEntity entity : homeCapabilityMappingEntities) {
                if ("103".equals(entity.getProjectType())) {
                    result.setIncinerationFinishCount(Double.valueOf(entity.getCount()));
                    if (!ObjectUtils.isEmpty(sumResult)) {

                        sumResult.setIncinerationFinishCount(new BigDecimal(Double.valueOf(entity.getCount()) + sumResult.getIncinerationFinishCount()).setScale(2,BigDecimal.ROUND_HALF_DOWN).doubleValue());
                    }
                    result.setIncinerationAimAmount(new BigDecimal(entity.getDesignHandle()).setScale(2,BigDecimal.ROUND_HALF_DOWN).doubleValue());
                    if (!ObjectUtils.isEmpty(sumResult)) {

                        sumResult.setIncinerationAimAmount(new BigDecimal(entity.getDesignHandle() + sumResult.getIncinerationAimAmount()).setScale(2,BigDecimal.ROUND_HALF_DOWN).doubleValue());
                    }
                }
                if ("104".equals(entity.getProjectType())) {
                    result.setKitchenFinishCount(Double.valueOf(entity.getCount()));


                    if (!ObjectUtils.isEmpty(sumResult)) {

                        sumResult.setKitchenFinishCount(new BigDecimal(Double.valueOf(entity.getCount()) + sumResult.getKitchenFinishCount()).setScale(2,BigDecimal.ROUND_HALF_DOWN).doubleValue());
                    }
                    result.setKitchenAimHandle(new BigDecimal(entity.getDesignHandle()).setScale(2,BigDecimal.ROUND_HALF_DOWN).doubleValue());
                    if (!ObjectUtils.isEmpty(sumResult)) {

                        sumResult.setKitchenAimHandle(new BigDecimal(entity.getDesignHandle() + sumResult.getKitchenAimHandle()).setScale(2,BigDecimal.ROUND_HALF_DOWN).doubleValue());
                    }
                }
            }
            homeRequestEntity.setMonth(lastMonth);
            HomeCapabilityMappingEntity homeCapabilityMappingEntities2 = capabilityAnalysisDao.endDealInPlace(homeRequestEntity);
            result.setLocationDisposed(homeCapabilityMappingEntities2.getLocationDisposed());
            if (!ObjectUtils.isEmpty(sumResult)) {

                sumResult.setLocationDisposed(new BigDecimal(homeCapabilityMappingEntities2.getLocationDisposed() + sumResult.getLocationDisposed()).setScale(2,BigDecimal.ROUND_HALF_DOWN).doubleValue());
            }
        }
        return resultList;
    }

    @Override
    public List<HomeCapabilityMappingEntity> endDealGetProjectList(HomeRequestEntity homeRequestEntity) {
        String code = String.valueOf(homeRequestEntity.getAreaCode());
        homeRequestEntity.setAreaCode(removeLast00(code));
        return capabilityAnalysisDao.endDealGetProjectList(homeRequestEntity);
    }

    @Override
    public HomeCapabilityMappingEntity endDealGetProject(String rowId) {
        return capabilityAnalysisDao.endDealGetProject(rowId);
    }

    /**
     * 去掉字符串末尾的00
     * @param code
     * @return
     */
    private String removeLast00(String code) {
        boolean isAppend = false;
        StringBuilder areaCode = new StringBuilder();
        for (int i = 1; i <= code.length(); i++) {
            if ((i % 2 == 0)) {
                String s = code.substring((code.length() - i), (code.length() - i + 2));
                if (!"00".equals(s)) {
                    areaCode.insert(0, s);
                    isAppend = true;
                }else {
                    if (isAppend) {
                        areaCode.insert(0, s);
                    }
                }
            }
        }
        return areaCode.toString();
    }
}
